Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scope based upsert or delete #1258

Closed
wants to merge 1 commit into from
Closed

Conversation

akostadinov
Copy link
Contributor

@akostadinov akostadinov commented Feb 28, 2024

When an index is defined, one can specify a scope. This scope is used to obtain records on initial indexing (ts:index) but is not respected by the model callbacks.

With this additional method one can easily implement ActiveRecord callbacks e.g.

    after_commit :index_object

    def index_object
      ThinkingSphinx::Processor.new(instance: instance).stage
    end

A little bit more efficient (preventing a database roundtrip on deletes

    after_commit :index_object, :on => %i(create update)
    after_commit :deindex_object, :on => :destroy

    def index_object
      ThinkingSphinx::Processor.new(instance: self).stage
    end

    def deindex_object
      ThinkingSphinx::Processor.new(instance: self).delete
    end

If certain models never use scopes for their indices, then #upsert can be called instead of #stage to avoid database roundtrips.

While on it, example background processing of indexing operations e.g.

    after_commit :background_index_object

    def background_index_object
      ThinkingIndexationWorker.perform_later(self.class, id) # call ThinkingSphinx::Processor#stage in worker
    end

fixes #1253
complements #1216 and #1215

@akostadinov
Copy link
Contributor Author

akostadinov commented Feb 28, 2024

Not sure why test / manticore (2.7, 5_2, mysql2, 6.0.0, manticore) (pull_request) failed. I don't see any change related to it and the postgres variant (along all other runners) pass. Maybe just rerun it?

@pat
Copy link
Owner

pat commented Jul 7, 2024

Thanks @akostadinov - I've rebased this to resolve the build failures (not your fault!), renamed the method from stage to sync, and merged it in. Hoping to get a release of the gem out this evening 🤞🏻

@pat pat closed this Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to remove records on update with real-time indexes
2 participants